<?php
namespace Home\Controller;
use Think\Controller;
class DeviceController extends Controller {
    public function index(){
    	$this->display();
    }
    public function getDeviceList(){
    	$res = D('device')->getDeviceList();
    	if(!$res){
    		return ajaxReturn(0,'获取失败');
    	}
    	return ajaxReturn(1,'获取成功',$res);
    }
    public function insert(){
    	$res = D('drive')->getDriveList();
    	$this->assign('drive',$res);
    	$res = D('command')->getCommandList();
    	$this->assign('command',$res);
    	$this->display();
    }
    public function insertDeviceInfo(){
    	if(!isset($_POST['device_name']) || !$_POST['device_name']){
    		return ajaxReturn(0,'传参失败');
    	}
    	$res = D('device')->insertDeviceInfo($_POST);
    	if(!$res){
    		return ajaxReturn(0,'修改失败');
    	}
    	if($_POST['drive_id']){
    		foreach($_POST['drive_id'] as $key => $value){
    			$dataDrive[] = array(
	    			'device_id' => $res,
	    			'drive_id' => $_POST['drive_id'][$key]
	    		);
    		}
    		$resDrive = D('device_drive')->insertDeviceDriveList($dataDrive);
    		if(!$resDrive){
	    		return ajaxReturn(0,'添加失败');
	    	}
    	}
    	if($_POST['command_id']){
    		foreach($_POST['command_id'] as $key => $value){
    			$dataCommand[] = array(
	    			'device_id' => $res,
	    			'command_id' => $_POST['command_id'][$key]
	    		);
    		}
    		$resCommand = D('device_command')->insertDeviceCommandList($dataCommand);
    		if(!$resCommand){
	    		return ajaxReturn(0,'添加失败');
	    	}
    	}
    	if(!$res){
    		return ajaxReturn(0,'添加失败');
    	}
    	return ajaxReturn(1,'添加成功');
    }
    public function revise(){
    	if(!isset($_GET['device_id']) || !$_GET['device_id']){
    		return ajaxReturn(0,'传参失败');
    	}
    	$res = D('drive')->getDriveList();
    	$this->assign('drive',$res);
    	$res = D('device_drive')->getDeviceDriveList($_GET);
    	foreach($res as $key => $value){
    		$temp .= $value['drive_id'] . ',';
    	}
    	$temp = substr($temp,0,strlen($temp)-1);
    	$this->assign('device_drive',$temp);
    	
    	$res = D('command')->getCommandList();
    	$this->assign('command',$res);
    	$res = D('device_command')->getDeviceCommandList($_GET);
    	$temp = '';
    	foreach($res as $key => $value){
    		$temp .= $value['command_id'] . ',';
    	}
    	$temp = substr($temp,0,strlen($temp)-1);
    	$this->assign('device_command',$temp);
    	
    	$res = D('device')->getDeviceInfo($_GET);
    	if(!$res){
    		return ajaxReturn(0,'添加失败');
    	}
    	$this->assign('device',$res);
    	$this->display();
    }
    public function reviseDeviceInfo(){
    	if(!isset($_POST) || !$_POST){
    		return ajaxReturn(0,'传参失败');
    	}
    	$cond['device_id'] = $_POST['device_id'];
    	$res = D('device')->reviseDeviceInfo($cond,$_POST);
    	if($res === false){
    		return ajaxReturn(0,'修改失败');
    	}
    	if($_POST['drive_id']){
    		$res = D('device_drive')->removeDeviceDriveList($cond);
	    	if($res === false){
	    		return ajaxReturn(0,'修改失败');
	    	}
    		foreach($_POST['drive_id'] as $key => $value){
    			$resDrive[] = array(
	    			'device_id' => $_POST['device_id'],
	    			'drive_id' => $_POST['drive_id'][$key]
	    		);
    		}
    		$resDrive = D('device_drive')->insertDeviceDriveList($resDrive);
    		if(!$resDrive){
	    		return ajaxReturn(0,'添加失败');
	    	}
    	}
    	if($_POST['command_id']){
    		$res = D('device_command')->removeDeviceCommandList($cond);
	    	if($res === false){
	    		return ajaxReturn(0,'修改失败');
	    	}
    		foreach($_POST['command_id'] as $key => $value){
    			$resCommand[] = array(
	    			'device_id' => $_POST['device_id'],
	    			'command_id' => $_POST['command_id'][$key]
	    		);
    		}
    		$resCommand = D('device_command')->insertDeviceCommandList($resCommand);
    		if(!$resCommand){
	    		return ajaxReturn(0,'添加失败');
	    	}
    	}
    	return ajaxReturn(1,'修改成功');
    }
    
    public function create($data){
    	$myfile = fopen("Public/file/newfile.sh", "w") or die("Unable to open file!");
    	fwrite($myfile,'#!/bin/sh'."\n");
		$data = explode(' ',$data);
		foreach($data as $k => $v){
			fwrite($myfile, $v."\n");
		}
		fclose($myfile);
    }
    
    public function send(){
    	$curl = curl_init();
		$path = dirname(dirname(dirname(dirname(__FILE__)))) . '/Public/file/newfile.sh';
		curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
		$data = array('file' => new \CURLFile(realpath($path)));
		curl_setopt($curl, CURLOPT_URL, $_SERVER['SERVER_ADDR'].'/dt200_front/php/waiyuan.php');
		curl_setopt($curl, CURLOPT_POST, 1 );
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$result = curl_exec($curl);
		cDebug($result);
		$error = curl_error($curl);
    }
}